﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SP = Microsoft.SharePoint.Client;
using SharePointExplorer.SharePoint;
using System.Threading;
using Microsoft.SharePoint.Client;
using SharePointExplorer.KinectControls;
using SharePointExplorer.Classes;
using FluidKit.Controls;
namespace SharePointExplorer.UserControls
{
    /// <summary>
    /// Interaction logic for DashboardItem.xaml
    /// </summary>
    public partial class DashboardItem : UserControl
    {
        App app = (App)Application.Current;
        private bool SitesVisible = false;
        public DashboardItem()
        {
            InitializeComponent();
            SharePointExplorer.Classes.KinectHelper.TogglePostureDetectedHandler(false);
            
            //var list = SharePointHelper.GetListCollection();
        }

        void DashboardItem_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        public DashboardItem(string ItemToLoad) :  this()
        {
            List<SP.List> list = null;
            app.MainWindowInstance.txtCurrentScreen.Text = ItemToLoad;
            this.Tag = ItemToLoad;
            if (ItemToLoad == "DocLibrary")
            {
                list = SharePointHelper.GetDocLibCollection();
                InitialSetup(list);
            }

            if (ItemToLoad == "List")
            {
                list = SharePointHelper.GetListCollection();
                InitialSetup(list);
            }


            if (ItemToLoad == "Sites")
            {
                var lists = SharePointHelper.GetSiteCollection();
                DashboardDetailsList.ItemsSource = lists;
                SitesVisible = true;
                app.MainWindowInstance.txtURLHeader.Visibility = Visibility.Visible;
                app.MainWindowInstance.txtCurrentURL.Visibility = Visibility.Visible;
                app.MainWindowInstance.StartOverButton.Visibility = Visibility.Visible;
            }
        }

        private void InitialSetup(List<SP.List> list)
        {
            DashboardDetailsList.ItemsSource = list;
            SpeakOperationAfter();
            app.MainWindowInstance.DeactivateButton.Visibility = Visibility.Visible;
            app.MainWindowInstance.HomeButton.Visibility = Visibility.Visible;
            app.MainWindowInstance.backButton.Visibility = Visibility.Collapsed;
        }

        private void SpeakOperationAfter()
        {
            if (SharePointUtils.MoveFlag)
            {
                SharePointExplorer.Classes.SpeechService SpeechSvc = new Classes.SpeechService();
                SpeechSvc.SpeakOperation(Classes.SpeechService.Operation.Move, Classes.SpeechService.OperationType.After);
            }
            else if (SharePointUtils.CopyFlag)
            {
                SharePointExplorer.Classes.SpeechService SpeechSvc = new Classes.SpeechService();
                SpeechSvc.SpeakOperation(Classes.SpeechService.Operation.Copy, Classes.SpeechService.OperationType.After);
            }
        }


        private void KinectButton_Click(object sender, RoutedEventArgs e)
        {
            if (!SitesVisible)
            {
                string listName = ((TextBlock)((Grid)((KinectButton)sender).Content).Children[1]).Text;
                app.CurrentList = listName;
                if (SharePointUtils.MoveFlag)
                    SharePointHelper.MoveFile(listName);
                else if (SharePointUtils.CopyFlag)
                    SharePointHelper.CopyFile(listName);
                else
                {
                    NavigationManager nm = new NavigationManager();
                    nm.NavigateContent(this, new DashboardSubItems(listName), Direction.RightToLeft);
                }
            }
            else
            {
                string siteLocation = ((TextBlock)((Grid)((KinectButton)sender).Content).Children[1]).Text;
                string siteUrl = SharePointHelper.SharePointClientContext.Url + "/" + siteLocation;
                var result = SharePointHelper.Authenticate(siteUrl);
                if (result)
                {
                    NavigationManager nm = new NavigationManager();
                    nm.NavigateContent(this, new Dashboard(), Direction.LeftToRight);
                }
            }
        }

        
    }
}
